#include "client.h"

int main(int argc, char *argv[]){
    //  ./client ip port
    ARGS_CHECK(argc, 3);

    // 建立套接字，获得 socket 文件的文件描述符 clientFd
    int clientFd = socket(AF_INET, SOCK_STREAM, 0);
    ERROR_CHECK(clientFd, -1, "socket");

    // 定义服务端 client 的网络地址 serverAddr
    struct sockaddr_in serverAddr;
    memset(&serverAddr,0, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    // argv[2] = 8888 uint16_t port = atoi(argv[2])
    serverAddr.sin_port = htons(atoi(argv[2]));
    // argv[1] = 127.0.0.1 const char * ip = argv[1]
    serverAddr.sin_addr.s_addr = inet_addr(argv[1]);


    //向服务器发出连接的请求
    int ret = connect(clientFd, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
    ERROR_CHECK(ret, -1, "connect");
    printf("Connection to server has completed.\n");

    //请求注册或登录
    if(Register(clientFd) == -1) {
        printf("register failure, please connect again\n");
        close(clientFd);
        return 0;
    }
    //设置flag值接收服务器端给出的提示，然后打印相关信息，提示用户登录或注册成功还是失败
    int flag;
    ret = recv(clientFd, &flag, sizeof(int), MSG_WAITALL);
    if(ret == 0) {
        printf("register failure, please connect again\n");
        close(clientFd);
        return 0;
    } else {
        printf("register success\n");
    }

    //登录成功则循环输入命令
    while(1){
        char command[1024] = {0};
        char pathname[1024] = {0};
        char srcName[1024] = {0};
        char destName[1024] = {0};
        //输入命令
        printf("Please input command:\n");
        memset(command, 0, sizeof(command));
        ret = read(STDIN_FILENO, command, sizeof(command));
        if(ret == -1) {
            continue;
        }
        //去掉尾端的换行符
        int lengthOfCommand = strlen(command);
        command[lengthOfCommand - 1] = 0;
        //分析命令
        ret = CommandAnalyse(command, pathname);
        if(ret == -1) {
            printf("illegal command, please input again:\n");
        }
        //如果是上传文件，先在本地查看是否有该文件，有的话再向服务器端发出命令
        if(ret == _PUTFILE) {
            int fileExist = FileExist(pathname, srcName, destName);
            if(fileExist == -1) {
                printf("file not exist\n");
                continue;
            }
        }
        //将命令以小火车形式发送给服务器
        Train_t train;
        train.lenth = strlen(command);
        strcpy(train.buf, command);
        Sendn(clientFd, &train, train.lenth + 4);

        //根据分析结果进行相应动作
        if(ret == _CDDIR) {
            ret = RecvFlagOnly(clientFd, command);
            //若返回值为-1，则客户端直接关闭
            if(ret == -1) {
                break;
            }
        } else if(ret == _LSDIR) {
            ret = RecvFlagAndMsg(clientFd, command);
            //若返回值为-1，则客户端直接关闭
            if(ret == -1) {
                break;
            }
        } else if(ret == _PWDDIR) {
            ret = RecvFlagAndMsg(clientFd, command);
            //若返回值为-1，则客户端直接关闭
            if(ret == -1) {
                break;
            }
        } else if(ret == _RMFILE) {
            ret = RecvFlagOnly(clientFd, command);
            //若返回值为-1，则客户端直接关闭
            if(ret == -1) {
                break;
            }
        } else if(ret == _MKDIR) {
            ret = RecvFlagOnly(clientFd, command);
            //若返回值为-1，则客户端直接关闭
            if(ret == -1) {
                break;
            }
        } else if(ret == _GETFILE) {
            ret = RecvFlagOnly(clientFd, command);
            //若返回值为-1，则客户端直接关闭
            if(ret == -1) {
                break;
            }
        } else if(ret == _PUTFILE) {
            ret = SendFile(srcName, destName, clientFd);
            if(ret == -1) {
                break;
            }
            ret = RecvFlagOnly(clientFd, command);
            if(ret == -1) {
                break;
            }
        }
    }
    //关闭连接
    close(clientFd);
    return 0;
}



